简单起见,我们直接把源码中已经有的系统 App 复制一份,然后在其基础上做修改。
具体的,把 packages/apps/Calculator
在同目录下复制一份,然后修改文件名为 packages/apps/HelloHalApp
Java 源文件只保留 packages/apps/Calculator/src/com/android/calculator2/Calculator.java
,其余都删除。
然后将 Calculator.java 改名为 MainActivity.java,同时修改包名,修改好后的完整路径 packages/apps/HelloHalApp/src/com/yuandaima/hellohal/MainActivity.java
接着将 MainActivity.java
源码修改如下:
package com.yuandaima.hellohal;
import android.app.Activity;
import android.os.Bundle;
import android.os.HelloHal;
import android.content.Context;
import android.util.Log;
import android.os.RemoteException;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HelloHal helloHal = (HelloHal)getSystemService("HelloService");
try {
helloHal.open();
helloHal.write("nihao");
Log.d("yuandaima_log",helloHal.read());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
然后修改 packages/apps/HelloHalApp/AndroidManifest.xml
:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yuandaima.hellohal">
<application
android:icon="@mipmap/ic_launcher_calculator"
android:label="@string/app_name"
android:theme="@style/CalculatorTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_CALCULATOR" />
</intent-filter>
</activity>
</application>
</manifest>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
实际就改下主 Activity 名。
接着修改编译配置文件 packages/apps/HelloHalApp/Android.mk
:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
res_dirs := res
LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dirs))
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := HelloHalApp
include $(BUILD_PACKAGE)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
最后把模块加入 product 中:
修改 build/target/product/aosp_x86_64.mk
PRODUCT_COPY_FILES += frameworks/native/data/etc/android.hardware.ethernet.xml:system/etc/permissions/android.hardware.ethernet.xml
$(call inherit-product, $(SRC_TARGET_DIR)/product/core_64_bit.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/product/aosp_base_telephony.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/board/generic_x86_64/device.mk)
include $(SRC_TARGET_DIR)/product/emulator.mk
ifdef NET_ETH0_STARTONBOOT
PRODUCT_PROPERTY_OVERRIDES += net.eth0.startonboot=1
endif
# Ensure we package the BIOS files too.
PRODUCT_PACKAGES += \
bios.bin \
vgabios-cirrus.bin \
HelloHalApp \ # 添加新添加的 App
# Overrides
PRODUCT_NAME := aosp_x86_64
PRODUCT_DEVICE := generic_x86_64
PRODUCT_BRAND := Android
PRODUCT_MODEL := AOSP on IA x86_64 Emulator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
最后编译运行模拟器:
source build/envsetup.sh
lunch aosp_x86-eng
emulator -kernel /tmp/kernel-qemu/x86_64-3.10.0/kernel-qemu
1
2
3
2
3
然后运行 App,查看 log:
adb shell logcat | grep yuandaima_log
1
完事儿。